package com.freesky.quartz.task;

import com.freesky.biz.rights.domain.RightsPayEntity;
import com.freesky.biz.rights.domain.RightsPayRefundEntity;
import com.freesky.biz.rights.mapper.RightsPayMapper;
import com.freesky.biz.rights.mapper.RightsPayRefundMapper;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.constant.CommonConstants;
import com.freesky.framework.web.service.SendAlarmService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.result.WxPayRefundResult;
import com.github.binarywang.wxpay.service.WxPayService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 权益退款
 */
@Component("rightsRefundTask")
public class RightsRefundTask {

    private static final Logger logger = LoggerFactory.getLogger(RightsRefundTask.class);

    @Autowired
    private SendAlarmService sendAlarmService;

    @Autowired
    private WxPayService payService;

    @Autowired
    private RightsPayMapper rightsPayMapper;

    @Autowired
    private RightsPayRefundMapper rightsPayRefundMapper;

    public void taskProcess() {
        logger.info("rightsRefund task start");
        List<RightsPayEntity> rightsPayList = rightsPayMapper.selectUnProcessList();
        for(RightsPayEntity rightsPay : rightsPayList){
            rightsPayMapper.updateProcessed(rightsPay.getId());
            refundPay(rightsPay);
        }
    }

    //微信退款
    public void refundPay(RightsPayEntity rightsPay){
        try{
            rightsPay.setRefundFlag("1");
            WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
            wxPayRefundRequest.setTransactionId(rightsPay.getTransactionId());
            wxPayRefundRequest.setOutTradeNo("PSBCSM"+rightsPay.getId());
            wxPayRefundRequest.setOutRefundNo(rightsPay.getId());
            wxPayRefundRequest.setTotalFee(Integer.valueOf(rightsPay.getAmt()));
            wxPayRefundRequest.setRefundFee(Integer.valueOf(rightsPay.getAmt()));
            wxPayRefundRequest.setNotifyUrl(CommonConstants.PAY_NOTIFY_URL + "/pay/notify/wechat/wechatRefund");
            WxPayRefundResult wxPayRefundResult = payService.refund(wxPayRefundRequest);

            RightsPayRefundEntity rightsPayRefund = new RightsPayRefundEntity();
            rightsPayRefund.setId(SeqUtil.getSeqId());
            rightsPayRefund.setTransactionId(wxPayRefundRequest.getTransactionId());
            rightsPayRefund.setTradeNo(wxPayRefundRequest.getOutTradeNo());
            rightsPayRefund.setRefundNo(wxPayRefundRequest.getOutRefundNo());
            rightsPayRefund.setRefundFee(String.valueOf(wxPayRefundRequest.getRefundFee()));

            rightsPayRefund.setResultCode(wxPayRefundResult.getResultCode());
            rightsPayRefund.setResultMsg(wxPayRefundResult.getReturnMsg());
            rightsPayRefund.setCreateTime(new Date());
            if(wxPayRefundResult.getResultCode().equalsIgnoreCase("SUCCESS")){
                rightsPayRefund.setRefundNo(wxPayRefundResult.getRefundId());
                rightsPay.setProcessed("2");
            } else {
                rightsPay.setProcessed("3");
            }
            rightsPayMapper.updatePayResult(rightsPay);
            rightsPayRefundMapper.insert(rightsPayRefund);

        } catch (Exception ioe){
            ioe.printStackTrace();
            logger.error(ioe.getMessage());
            sendAlarmService.sendAlarmMail(CommonConstants.ALARM_EMAIL,"【异常】JOB-权益退款请求-refundPay",ioe.getMessage());
        }
    }
}
